package com.ssu.judea.travlog.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ssu.judea.travlog.TravlogApplication;
import com.ssu.judea.travlog.vo.DatePost;
import com.ssu.judea.travlog.vo.Member;
import com.ssu.judea.travlog.vo.Result;
import com.ssu.judea.travlog.vo.ResultPost;

public class JSONParseHelper {
	
	public static JSONObject getActionJSONObject(String currentAction){
		
		JSONObject jsonAction = new JSONObject();
		try {
			jsonAction.put(TravlogApplication.PARAMS_ACTION, currentAction);
		} catch (JSONException e) {
			Log.e("changdoc", JSONParseHelper.class.getName() + "getActionJSONObject" +e.getMessage());
		}
		
		return jsonAction;
	}
	public static Member parseToMember(JSONObject input){
		Member member = new Member();
		
		if(input!=null){
			try{
				member.setMemberseq(input.getInt("member_seq"));
				member.setProfileurl(input.getString("member_profile_url"));
				member.setTwitterid(input.getString("member_twitter_id"));
				return member;
			}catch(JSONException e){
				Log.v("changdoc",JSONParseHelper.class.getName() + "parseToMember" + e.getMessage());
				return null;
			}
		}else{
			return null;
		}		
	}
	
	public static Result parseToResult(JSONObject input){
		Result result = new Result();
		
		if(input!=null){
			try{
				result.setResult(input.getString("result"));
				result.setMessage(input.getString("message"));
				return result;
			}catch(JSONException e){
				Log.v("changdoc",JSONParseHelper.class.getName() + "parseToResult" + e.getMessage());
				return null;
			}
		}else{
			return null;
		}
	}
	
	
	public static ArrayList<DatePost> parseToDatePostList(JSONArray input){
		ArrayList<DatePost> result = new ArrayList<DatePost>();
		
		for(int i = 0; i<input.length();i++){
			DatePost curDateObj = new DatePost();
			try {
				JSONObject curJson = input.getJSONObject(i);
				curDateObj.setMemberseq(curJson.getInt("memberseq"));
				curDateObj.setTwitterid(curJson.getString("twitterid"));
				curDateObj.setImageurl(curJson.getString("imageurl"));
				curDateObj.setLatitude(curJson.getDouble("latitude"));
				curDateObj.setLongitude(curJson.getDouble("longitude"));
				curDateObj.setPostscount(curJson.getInt("postscount"));
				curDateObj.setRegdate(curJson.getString("regdate"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			result.add(curDateObj);
		}
		return result;
	}
	
	public static ArrayList<ResultPost> parseToResultPostList(JSONArray input){
		ArrayList<ResultPost> result = new ArrayList<ResultPost>();
		
		for(int i = 0; i<input.length();i++){
			ResultPost curDateObj = new ResultPost();
			try {
				JSONObject curJson = input.getJSONObject(i);
				curDateObj.setMemberseq(curJson.getInt("memberseq"));
				curDateObj.setTwitterid(curJson.getString("twitterid"));
				curDateObj.setLatitude(curJson.getDouble("latitude"));
				curDateObj.setLongitude(curJson.getDouble("longitude"));
				curDateObj.setRegdate(curJson.getString("regdate"));
				curDateObj.setRegtime(curJson.getString("regtime"));
				curDateObj.setOpen(curJson.getInt("open"));
				String imageurl = curJson.getString("attachimageurl");
				curDateObj.setAttachimageurl(imageurl);
				curDateObj.setAttachthumbnailurl(imageurl.substring(0, imageurl.lastIndexOf("."))+"_s"+imageurl.substring(imageurl.lastIndexOf(".")));
				curDateObj.setContents(curJson.getString("contents"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			result.add(curDateObj);
		}
		return result;
	}
	
	public static ArrayList<ResultPost> parseToResultRecentPostList(JSONArray input){
		ArrayList<ResultPost> result = new ArrayList<ResultPost>();
		
		for(int i = 0; i<input.length();i++){
			ResultPost curDateObj = new ResultPost();
			try {
				JSONObject curJson = input.getJSONObject(i);
				curDateObj.setMemberimageurl(curJson.getString("memberimageurl"));
				curDateObj.setMemberseq(curJson.getInt("memberseq"));
				curDateObj.setPostscount(curJson.getInt("postscount"));
				curDateObj.setTwitterid(curJson.getString("twitterid"));
				curDateObj.setLatitude(curJson.getDouble("latitude"));
				curDateObj.setLongitude(curJson.getDouble("longitude"));
				curDateObj.setRegdate(curJson.getString("regdate"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			result.add(curDateObj);
		}
		return result;
	}
	
}
